/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package TarryDAOS;

import Herramientas.PasarelaDatos;
import Herramientas.ResultSets;
import TarryDTOS.PartnerDTO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alan Gonzalez
 */
public class PartnerDAO {

    private ResultSets res;
    private PasarelaDatos db;

    public PartnerDAO() {
        res = new ResultSets();
        db = new PasarelaDatos();
    }

    /**
     * ALAN GONZALEZ ESTE METODO DEVUELVE TODOS LOS PARTNES
     *
     * @return
     */
    public Vector Partners() {
        Vector vP = new Vector();
        db.openDatabase();
        String sql = "SELECT * FROM partners ";
        ResultSet rst = db.executeQuery(sql);
        try {
            while (rst.next()) {
                PartnerDTO p = new PartnerDTO();
                p = res.partnerDTO(rst);
                vP.add(p);
            }
        } catch (SQLException ex) {
            Logger.getLogger(PartnerDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        db.closeDatabase();
        return vP;
    }

    /**
     * ALAN GONZALEZ UN PROVEEDOR VENDEDOR ETC POR ID
     *
     * @param id
     * @return
     */
    public PartnerDTO unPartnerPorId(int id) {
        PartnerDTO p = new PartnerDTO();
        String sql = "SELECT * FROM partners WHERE partners.id=" + id;
        db.openDatabase();
        ResultSet rst = db.executeQuery(sql);
        try {
            if (rst.next()) {
                p = res.partnerDTO(rst);
            } else {
                p = null;
            }
        } catch (SQLException ex) {
            Logger.getLogger(PartnerDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        db.closeDatabase();
        return p;
    }

    /**
     * ACTUALIZA EL EMAIL DEL PARTNER 
     * @param idPartner
     * @param email 
     */
    public void actualizaEmailPartner(int idPartner, String email) {
        String sql = "UPDATE partners SET partners.email = '" + email + "' WHERE partners.id=" + idPartner;
        db.openDatabase();
        db.execute(sql);
        db.closeDatabase();
    }

}
